<?php
class comment_controller_index extends Controller{
	/**
	 * 
	 *
	 * @var comment_model_resources
	 */
	var $res;
	/**
	 * 
	 *
	 * @var comment_model_post
	 */
	var $post;
	/**
	 * 
	 *
	 * @var comment_model_thread
	 */
	var $thread;
	 /**
	 * 
	 *
	 * @var comment_model_recive
	 */
	var $recive;
	/**
	 * 
	 *
	 * @var comment_model_data
	 */
	var $query;
	/**
	 * 
	 *
	 * @var data
	 */
	var $data;
	/**
	 * 
	 *
	 * @var login_uid
	 */
	var $login_uid;
	/**
	 * 
	 *
	 * @var res_uid
	 */
	var $res_uid;
	/**
	 * 
	 *
	 * @var res_id
	 */
	var $res_id;
		/**
	 * 
	 *
	 * @var strtime
	 */
	var $strtime;
	/**
	 * 
	 *
	 * @var show
	 */
	var $show;
	
	var $configkey = '178.com';
	
	
	public function __construct($app, $controller)
	{
		parent::__construct($app, $controller);
		$this->headerView = $this->viewDir."/_header.phtml";
	    
	}

	public function index_action(){
		
		$this->requireLogin();
		$uid = self::$login['uid'];
	
	}
	
	/**
	 * 我评论列表
	 *
	 */
	public function my_comm_action(){
		$this->requireLogin();
		$uid = self::$login['uid'];
		
	}
	
	//添加一条资源
	public function add_res_action($mark,$app,$control,$action,$id,$uid,$res_name)
	{
		$this->res = new comment_model_resources();
		$this->requireLogin();

		$arguments = array('res_id'=>$mark, 'res_name'=>$res_name,'uid'=>$uid,'app'=>$app, 'control'=>$control, 'act'=>$action,'id'=>$id,'deleted'=>0);
		
		$this->res->add_resources($arguments);
	
	}
	
	//显示评论列表
	public function view_comment_action()
	{

		$this->res = new comment_model_resources();
		$this->query = new comment_model_data();
		$this->_renderJSON();
		$this->show = 1;
		$this->login_uid = self::$login['uid'];

		$app		= isset($_POST['app']) ?  $_POST['app'] : '';
		$control	= isset($_POST['control']) ?  $_POST['control'] : '';
		$action		= isset($_POST['act']) ?  $_POST['act'] : '';
		$id			= isset($_POST['act']) ? (int) $_POST['id'] : 0;
		$my			=	$_POST['my'] ? $_POST['my']:0;
		$name_array = $_SERVER['config']['comments'];
		$this->res_uid = $uid = intval($_POST['res_uid']);
		$code		= trim($_POST['code']);
		$res_name	= $name_array[$_POST['app']][$_POST['control']];

		if (!$res_name) {
			echo '资源不存在'; $this->show=0 ;
		} else {
			$this->res_id = md5($app.$control.$action.$id);
			$result = $this->res->check_res($this->res_id);
			if ($result) {	//资源id存在
				$this->res_uid = $result['uid'];

				//判断用户权限
//				$privacy_data = friends_util_cache::getUserInfo($result['uid']);
//				if ($this->login_uid != $result['uid']) $this->query->privacy($this->login_uid, $result, $privacy_data);

				//取得评论的列表
				if (Cache::getInstance()->load($this->res_id)) {
					$post_data_str	=	Cache::getInstance()->load($this->res_id);
					$post_data		= unserialize($post_data_str);
				} else {
					$post_data = $this->query->get_comment_data($this->res_id,$my);
					$post_data_str	= serialize($post_data);
					Cache::getInstance()->save($post_data_str,$this->res_id,array(),300);
				}
				if($post_data)	$this->data = $post_data;
				else  $this->data ='';
			} else {
				$check = comment_util_comment::explainTheCode($app,$control,$action,$id,$code);
				if ($check) {  //通过验证
					$this->add_res_action($this->res_id,$app,$control,$action,$id,$uid,$res_name);
					$this->data = '';
				} else { echo '资源不存在'; $this->show=0 ; }
			}
		}
		$this->strtime = substr(time(),-2);
		if ($app == 'index' && $control == 'index' && $action == 'index')  $this->view = 'board_comment';
		else 	$this->view = 'show_comment';
//		var_dump($this->data);
	}
	
	//添加一条评论
	public function send_comment_action()
	{
		$this->res = new comment_model_resources();
		$this->post = new comment_model_post();
		$this->thread = new comment_model_thread();
		$this->query = new comment_model_data();
		$this->_renderJSON();
		$this->requireLogin();
		$post = &$_POST;
		$uid = self::$login['uid'];
		$username = self::$login['nickname'];
		if(!$uid){ echo '请先登陆！';
		}else{ $post['author_id'] = $uid;  $post['author_name'] = $username; $post['type'] = 1;

		$res_data = $this->res->check_res($post['res_id']);

		if ($res_data){
			//判断用户权限
			/*$privacy_data = friends_util_cache::getUserInfo($res_data['uid']);		
			if ($uid != $res_data['uid']) $this->query->privacy($uid, $res_data, $privacy_data);*/

			
			$this->res->update_res_comment($post['res_id'],'');	//评论数加1
			$thread_id = $this->thread->add_thread($post);
			if ($thread_id){
				$post_id	=	$this->post->add_post($post,$thread_id);
				
				//消息接口
				$type = false;
				//添加缓存
				$post_data = $this->query->get_comment_data($post['res_id']);
				$post_data_str = serialize($post_data);
				Cache::getInstance()->save($post_data_str,$post['res_id'],array(),3000);	
				//清除动态缓存
				$key	=	md5($thread_data['res_id'].$this->configkey);
				Cache::getInstance()->remove($key);	
			}
		}
		$this->act = $res_data;
		$this->ret = self::success($res_data);
		}
	}
	
	//添加一条回复
	public function reply_comment_action()
	{
		$this->res	= new comment_model_resources();
		$this->post	= new comment_model_post();
		$this->thread	= new comment_model_thread();
		$this->query	= new comment_model_data();
		$this->_renderJSON();
		$this->requireLogin();

		$uid = self::$login['uid'];
		$username = self::$login['nickname'];
		$post					=	$_POST;
		$post['author_id']		=	$uid;
		$post['type']			=	0;
		$post['author_name']	=	$username;
		$thread_id				=	$post['thread_id'];

		//返回帖子的相关资源 信息
		$thread_data = $this->thread->get_thread_data($thread_id);
		$res_data = $this->res->check_res($thread_data['res_id']);
		
		//判断用户权限
		/*$privacy_data = friends_util_cache::getUserInfo($res_data['uid']); 	
		if ($uid != $res_data['uid']) $this->query->privacy($uid, $res_data, $privacy_data);*/
		
		$post['res_id'] = $thread_data['res_id'];
		$this->thread->update_reply_time($thread_id);
		$post_id = $this->post->add_post($post,$thread_id);
		
		if ($post_id){
			//添加评论数
			$this->res->update_res_comment($thread_data['res_id'],'');
			
			
			//刷新缓存
			$post_data = $this->query->get_comment_data($thread_data['res_id']);
			$post_data_str = serialize($post_data);
			Cache::getInstance()->save($post_data_str,$thread_data['res_id'],array(),3000);
						//清除动态缓存
			$key	=	md5($thread_data['res_id'].$this->configkey);
			Cache::getInstance()->remove($key);	

		}
		
		$this->act = $res_data ;
		$this->ret = self::success($this->act);
	}
	
	//删除一条评论
	public function delete_comment_action()
	{
		$this->res = new comment_model_resources();
		$this->post = new comment_model_post();
		$this->thread = new comment_model_thread();
		$this->query = new comment_model_data();
		$this->_renderJSON();
		$this->requireLogin();
		$uid			= self::$login['uid'];
		$thread_id		= isset($_POST['thread_id']) ? (int) $_POST['thread_id'] : 0;
		$thread_data	= $this->thread->get_thread_data($thread_id);
		$res_data		= $this->res->check_res($thread_data['res_id']);

		if (!$thread_data || $thread_data['deleted'] == 1) {
			
			$this->ret = self::failure('帖子不存在或已删除');
			
		} elseif ($thread_data['author_id'] != $uid && $uid != $res_data['uid'] ) {
			$this->act = $thread_data['author_id'].'||'.$uid;
			$this->ret = self::success($this->act);
		} else {
			$thread_num = $this->post->delete_comment($thread_id);
			
			//减去这个帖子的回复数
			$this->res->update_res_comment($thread_data['res_id'],$thread_num); 
			
			//动态评论数接口
			$appId = $this->query->getModelIdByApp($res_data['app']);
			if($appId) feed_util_log::decrCommentNums($appId, $res_data['id']);
			
			$this->thread->delete_thread_data($thread_id);
			
			$post_data = $this->query->get_comment_data($thread_data['res_id']);
			$post_data_str = serialize($post_data);
			Cache::getInstance()->save($post_data_str,$thread_data['res_id'],array(),3000);
			
			//清除动态缓存
			$key	=	md5($thread_data['res_id'].$this->configkey);
			Cache::getInstance()->remove($key);
			
			//消息接口
			sms_util_comment::delCommentLog($thread_data['thread_id'],$thread_data['author_id'],1);			
			
			$this->ret = self::success($res_data);
		}
	} 
	
	//删除一条评论回复
	public function delete_comment_post_action()
	{
		$this->res = new comment_model_resources();
		$this->post = new comment_model_post();
		$this->thread = new comment_model_thread();
		$this->query = new comment_model_data();
		$this->_renderJSON();
		$uid		= self::$login['uid'];
		$post_id	= isset($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
		$thread_id	= isset($_POST['thread_id']) ? (int) $_POST['thread_id'] : 0;		

		$thread_data = $this->thread->get_thread_data($thread_id);
		$res_data = $this->res->check_res($thread_data['res_id']);
		$post_data_id =$this->post->getPostDataById($post_id);
		

		if ($uid == $res_data['uid'] || $uid == $thread_data['author_id'] || $uid == $post_data_id['author_id'] ) {

			$this->res->update_res_comment($thread_data['res_id'],1);
			$this->post->delete_comment_post($uid,$post_id);
			
			//动态评论数接口
			$appId = $this->query->getModelIdByApp($res_data['app']);
			if($appId) feed_util_log::decrCommentNums($appId, $res_data['id']);
			
			//刷新缓存
			$post_data = $this->query->get_comment_data($thread_data['res_id']);
			$post_data_str = serialize($post_data);
			Cache::getInstance()->save($post_data_str,$thread_data['res_id'],array(),3000);		
			sms_util_comment::delCommentLog($thread_data['thread_id'],$thread_data['author_id'],0);
			
			//清除动态缓存
			$key	=	md5($thread_data['res_id'].$this->configkey);
			Cache::getInstance()->remove($key);
			
			$this->ret = self::success($res_data);
			
		}
	}
	
	/**
	 * 动态 显示评论列表
	 *
	 * @return array|null
	 */
	public function  get_comments_action()
	{
		$this->res	= new comment_model_resources();
		$this->post	= new comment_model_post();
		$this->thread	= new comment_model_thread();
		$this->query	= new comment_model_data();
		$this->_renderJSON();
		$aid = isset($_POST['aid']) ? (int) $_POST['aid'] : 0;	//应用标示ID
		$uid = isset($_POST['uid']) ? (int) $_POST['uid'] : 0;	//用户ID
		$tid = isset($_POST['tid']) ? (int) $_POST['tid'] : 0;	//应用模块内主键ID
		
		$res_data = $this->query->makeResidByModel($aid, $tid, $uid);
		$result		= '';
		$post_data	= array();
		if ($this->res->check_res($res_data['res_id'])) {
				$key	=	md5($res_data['res_id'].$this->configkey);
				if ($result = Cache::getInstance()->load($key)) {
						$post_data_str	=	$result;
						$post_data		=	unserialize($post_data_str);
					} else {
						$post_data 		= $this->query->getCommentData($res_data['res_id']);
						$post_data_str	= serialize($post_data);
						Cache::getInstance()->save($post_data_str,$key,array(),0);									
					}
					
		} else {
			$name_array = $_SERVER['config']['comments'];
			$res_name = $name_array[$res_data['app']][$res_data['control']];
			$this->add_res_action($res_data['res_id'], $res_data['app'], $res_data['control'], $res_data['act'], $tid, $uid, $res_name);
		}
		$data = $post_data ? $post_data : $res_data;
		$this->ret = self::success(array('comments' => $post_data, 'res' => $res_data));
	}
	
	/**
	 * 添加评论/回复
	 *
	 */
	public function submit_comments_action()
	{
		$this->res	= new comment_model_resources();
		$this->post	= new comment_model_post();
		$this->thread	= new comment_model_thread();
		$this->query	= new comment_model_data();
		$this->_renderJSON();
		$this->requireLogin();
		$post					=	$_P0ST;
		$post['res_id']			=	isset($_POST['res_id']) ? (string) $_POST['res_id'] : '';		//资源ID
		$post['type']			=	isset($_POST['type']) ? 	(int) $_POST['type'] : 1;			//评论：1 回复：0
		$post['thread_id']		=	isset($_POST['thread_id']) ? (int) $_POST['thread_id'] : 0;		//评论ID 	添加回复时需提供该参数
		$post['content']		=	isset($_POST['content']) ?  $_POST['content'] : '';
		$post['content']		=	$this->query->regular($_POST['content']);
		$post['author_id']		=	$uid		=	self::$login['uid'];
		$post['author_name']	=	$username 	= 	self::$login['nickname'];

		if ($post['type'] == 1 ) {
			$res_data		=	$this->res->check_res($post['res_id']);
		//判断用户权限
		$privacy_data = friends_util_cache::getUserInfo($res_data['uid']); 	
		if ($uid != $res_data['uid']) $this->query->privacy($uid, $res_data, $privacy_data);
		
			if ($res_data) {

				$this->res->update_res_comment($post['res_id'],'');	//评论数加1
				$thread_id			=	$this->thread->add_thread($post);
				$post['thread_id']	=	$thread_id;
				$post_id	=	$this->post->add_post($post,$thread_id);
				
				//动态评论数接口
				$appId = $this->query->getModelIdByApp($res_data['app']);
				if($appId){ 
					feed_util_log::incrCommentNums($appId, $res_data['id']);
				}
				
				//消息接口
				$type	=	false;
				if('index'== $res_data['app'] && 'index' == $res_data['control'] && 'index'== $res_data['act'])  {$type = true; }
				$para	=	array('thread_id'=>$thread_id,'res_uid'=>$res_data['uid'],'recom_uid'=>'');

				sms_util_comment::setUnReadComment($uid,$para,$type);
				$array	=	array('thread_id'	=>	$thread_id,
									'uid'		=>	$uid,
									'app'		=>	$res_data['app'],
									'control'	=>	$res_data['control'],
									'act'		=>	$res_data['act'],
									'rid'		=>	$res_data['id'],
									'authorid'	=>	$res_data['uid']
								);
				sms_util_comment::upCommentLog($array,1);
				Cache::getInstance()->remove($post['res_id']);
			}

			//添加缓存
			$post_data		= $this->query->getCommentData($post['res_id']);

			$post_data_str	= serialize($post_data);
			$key			= md5($post['res_id'].$this->configkey);
			Cache::getInstance()->save($post_data_str,$key,array(),0);		

		} else {
			//返回帖子的相关资源 信息
			$thread_data	=	$this->thread->get_thread_data($post['thread_id']);
			$res_data		=	$this->res->check_res($thread_data['res_id']);
			//判断用户权限
		    $privacy_data = friends_util_cache::getUserInfo($res_data['uid']); 	
			if ($uid != $res_data['uid']) $this->query->privacy($uid, $res_data, $privacy_data);


			$post['res_id'] = $thread_data['res_id'];
			$this->thread->update_reply_time($post['thread_id']);
			$post_id	=	$this->post->add_post($post,$post['thread_id']);

			//添加评论数
			$this->res->update_res_comment($thread_data['res_id'],'');
			
			//动态评论数接口
			$appId = $this->query->getModelIdByApp($res_data['app']);
			if($appId) {
				feed_util_log::incrCommentNums($appId, $res_data['id']);
			}

			//消息接口
			$type = false;
			if('index'== $res_data['app'] && 'index' == $res_data['control'] && 'index'== $res_data['act']) $type = true;
			sms_util_comment::setUnReadComment($uid,$para = array('thread_id'=>$thread_id,'res_uid'=>$res_data['uid'],'recom_uid'=>''),$type);
			$array	=	array('thread_id'	=>	$post['thread_id'],
									'uid'	=>	$uid,
									'app'	=>	$res_data['app'],
									'control'=>	$res_data['control'],
									'act'	=>	$res_data['act'],
									'rid'	=>	$res_data['id'],
									'authorid'=>$res_data['uid']
								);
			sms_util_comment::upCommentLog($array,0);
			
			//刷新缓存
			$post_data		=	$this->query->getCommentData($thread_data['res_id']);
			$post_data_str	=	serialize($post_data);
			$key			=	md5($thread_data['res_id'].$this->configkey);
			Cache::getInstance()->save($post_data_str,$key,array(),0);
			Cache::getInstance()->remove($thread_data['res_id']);
			
		}
		
				
				//评论动态
				$app_model = $res_data['app'];
				if ($app_model == 'album' && $res_data['act'] == 'photo_list') {
					$app_model = 'album_list';
				}
				if ($app_model == 'cite' && $res_data['act'] == 'viewAlbum') {
							$app_model = 'cite_album';
				}
				$obj =  isset($_SERVER['config']['resource2']['interface'][$res_data['app']]) ? $_SERVER['config']['resource2']['interface'][$app_model] : 0  ;
				if (class_exists($obj)) {
					$result = call_user_func_array(array($obj,'getResourceInfoById'),$res_data['id']);
					if ($result['uid']) {
						$userInfo = friends_util_cache::getUserInfo($result['uid']);
						$feed_data = array ('creat_uid'			=>  $result['uid'],
											'creat_username'	=>	$userInfo['username'],
											'title'				=>	$result['title'],
											'img'				=>	$result['img'],
											'content'			=>	$post['content'],				
											'url'				=>  							$_SERVER['config']['resource2']['url'][$app_model].$res_data['id']."/uid/" . $result['uid'],
						 );							
						feed_util_send::save($uid,$username,$appId,$res_data['id'],$feed_data,9);									 
					}
				}					
						
						
		//评论数
		$data = Cache::getInstance()->remove(md5($res_data['res_id']));
		
		$post['post_id'] = $post_id;
		$post['create_time'] = time();
		$this->ret = self::success($post);
	}
	
	/* 动态 删除留言 
		参数 type  为 1  删除主贴： thread_id 
			type  为 0  删除回复： thread_id  post_id
	*/
	public function deleteCommentPost_action() {
		$type = isset($_POST['type']) ? (int) $_POST['type'] : 0;
		if ($type) {			
			$this->delete_comment_action();						
		} else {

			$this->delete_comment_post_action();
			
		}
	}
	
	
}
